﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CarlosMisfortune.model
{
    public class AnimationStrip
    {
        #region Fields
        private Dictionary<string, Rectangle> m_data;
        private string[] m_nameList;

        private Texture2D m_texture;
        private int m_frameHeight;
        private int m_frameWidth;
        
        private int m_frameCount;

        private float m_frameTimer = 0.0f;
        private float m_frameDelay = 0.05f;

        private int m_currentFrame;
        private int m_currentRow;
        private int m_currentColumn;

        private bool m_loopAnimation = true;
        private bool m_finishedPlaying = false;

        private string m_name;
        private string m_nextAnimation;
        #endregion

        #region Properties
        public Dictionary<string, Rectangle> Data
        {
            get { return m_data; }
            set { m_data = value; }
        }

        public int FrameCount
        {
            get 
            {
                if (Data != null)
                {
                    return Data.Count;
                }
                else
                {
                    return 1;       
                }
            }
        }

        public int CurrentFrame
        {
            get { return m_currentFrame; }
            set { m_currentFrame = value; }
        }

        public Point FrameSize
        {
            get
            {
                return new Point(
                    Data[m_nameList[m_currentFrame]].Width,
                    Data[m_nameList[m_currentFrame]].Height);
            }
        }

        public Rectangle CurrentRectangle
        {
            get
            {
                if (m_nameList == null)
                {
                    if (Data != null)
                    {
                        m_nameList = Data.Keys.ToArray();
                    }
                    else
                    {
                        return new Rectangle(m_texture.Bounds.X, m_texture.Bounds.Y, m_texture.Width, m_texture.Height);
                    }
                    
                }
                return Data[m_nameList[m_currentFrame]];
            }
        }

        public Texture2D Texture
        {
            get { return m_texture; }
            set { m_texture = value; }
        }

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        public string NextAnimation
        {
            get { return m_nextAnimation; }
            set { m_nextAnimation = value; }
        }

        public bool LoopAnimation
        {
            get { return m_loopAnimation; }
            set { m_loopAnimation = value; }
        }

        public bool FinishedPlaying
        {
            get { return m_finishedPlaying; }
            set { m_finishedPlaying = value; }
        }

        public float Framelength
        {
            get { return m_frameDelay; }
            set { m_frameDelay = value; }
        }

        public Rectangle FrameRectangle
        {
            get
            {
                return new Rectangle(
                    m_currentColumn * m_frameWidth,
                    m_currentRow * m_frameHeight,
                    m_frameWidth,
                    m_frameHeight);
            }
        }
        #endregion

        #region Constructor
        public AnimationStrip(Texture2D a_texture, Dictionary<string, Rectangle> a_xmlData, string a_name)
        {
            m_currentFrame = 0;
            m_texture = a_texture;
            m_data = a_xmlData;

            //gets the frame data from the loaded xml. assumes all frames are the same size.            
            m_frameWidth = m_data.Values.First().Width;
            m_frameHeight = m_data.Values.First().Height;
            m_frameCount = m_data.Count; ;
            m_name = a_name;
        }

        public AnimationStrip(Texture2D a_texture, int a_frameWidth, int a_frameHeight, int a_frameCount, string a_name)
        {
            m_currentFrame = 0;
            m_texture = a_texture;
            m_data = null;
            m_frameWidth = a_frameWidth;
            m_frameHeight = a_frameHeight;
            m_frameCount = a_frameCount;
            m_name = a_name;
        }

        #endregion

        #region Public Methods
        public void Play()
        {
            m_currentFrame = 0;
            m_currentRow = 0;
            m_currentColumn = 0;
            m_finishedPlaying = false;
        }

        public void Update(GameTime a_gameTime)
        {
            float elapsedTime = (float)a_gameTime.ElapsedGameTime.TotalSeconds;

            m_frameTimer += elapsedTime;

            if (m_frameTimer >= m_frameDelay)
            {
                if (m_currentFrame < (FrameCount - 1))
                {
                    m_currentFrame++;
                }
                else
                {
                    if (m_loopAnimation)
                    {
                        m_currentFrame = 0;
                    }
                    else
                    {
                        m_currentFrame = FrameCount - 1;
                        m_finishedPlaying = true;
                    }
                }
                m_frameTimer = 0.0f;
            }
        }
        #endregion
    }
}
